package com.lineage.server.command.executor;

import com.lineage.server.datatables.lock.AccountReading;
import com.lineage.server.datatables.lock.IpReading;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_PacketBoxGm;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1AccountBanKick implements L1CommandExecutor {
  private static final Log _log = LogFactory.getLog(L1AccountBanKick.class);
  
  public static L1CommandExecutor getInstance() {
    return new L1AccountBanKick();
  }
  
  public void execute(L1PcInstance pc, String cmdName, String arg) {
    try {
      String gmName = "";
      if (pc == null) {
        _log.warn("系统命令执行: " + cmdName + " 帐号封锁:" + arg);
        gmName = "系统命令";
      } else {
        gmName = String.valueOf(String.valueOf(pc.getName())) + "命令";
      } 
      L1PcInstance target = World.get().getPlayer(arg);
      if (target != null) {
        String info = String.valueOf(String.valueOf(target.getName())) + " 该人物帐号完成封锁。";
        if (pc == null) {
          _log.warn(info);
        } else {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage(info));
        } 
        start(target, String.valueOf(String.valueOf(gmName)) + ":L1AccountBanKick 封锁帐号");
      } else {
        boolean account = AccountReading.get().isAccount(arg);
        if (account) {
          IpReading.get().add(arg, String.valueOf(String.valueOf(gmName)) + ":L1AccountBanKick 封锁帐号");
          return;
        } 
        if (pc == null) {
          _log.error("指令异常: 这个命令必须输入正确帐号名称才能执行。");
        } else {
          int mode = 7;
          pc.sendPackets((ServerBasePacket)new S_PacketBoxGm(pc, 7));
        } 
      } 
    } catch (Exception e) {
      if (pc == null) {
        _log.error("错误的命令格式: " + getClass().getSimpleName());
      } else {
        _log.error("错误的GM指令格式: " + getClass().getSimpleName() + " 执行的GM:" + pc.getName());
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(261));
      } 
    } 
  }
  
  private void start(L1PcInstance target, String info) {
    IpReading.get().add(target.getAccountName(), info);
    target.getNetConnection().kick();
  }
}
